package com.testmanager.testmananger.shuiyou.strCompare.service;

import com.testmanager.testmananger.foundation.dto.PageableDto;
import com.testmanager.testmananger.foundation.mapper.PageBaseMapper;
import com.testmanager.testmananger.foundation.util.FlakeIdGenerator;

import com.testmanager.testmananger.shuiyou.common.util.ExcelOperte;
import com.testmanager.testmananger.shuiyou.strCompare.domain.SyCompareScriptKeyEntity;
import com.testmanager.testmananger.shuiyou.strCompare.domain.SyCompareTaskEntity;
import com.testmanager.testmananger.shuiyou.strCompare.domain.SySqlCompareScriptEntity;
import com.testmanager.testmananger.shuiyou.strCompare.dto.CompareScriptDto;
import com.testmanager.testmananger.shuiyou.strCompare.dto.ExcelReadSqlScriptDto;
import com.testmanager.testmananger.shuiyou.strCompare.dto.SqlCompareScriptSearchDto;
import com.testmanager.testmananger.shuiyou.strCompare.mapper.CompareScriptMappre;
import com.testmanager.testmananger.shuiyou.strCompare.respository.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;


import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SqlCompareScriptService {

    @Autowired
    SySqlCompareScriptCustom sqlCompareScriptCustom;

    @Autowired
    SySqlCompareRepository sqlCompareRepository;

    @Autowired
    SyProjectRepository syProjectRepository;

    @Autowired
    SyCompareScriptKeyRepository scriptKeyRepository;// 1.锚点名   2.变量名

    @Autowired
    SqlCompareTaskService taskService;

    @Autowired
    SyCompareTaskRepository taskRepository;

    @Autowired
    FlakeIdGenerator flakeIdGenerator;

    public ResponseEntity search(SqlCompareScriptSearchDto sqlCompareScriptSearchDto){

        log.info("search sqlCompareScriptSearchDto:{}",sqlCompareScriptSearchDto);
        Page<SySqlCompareScriptEntity> page = sqlCompareScriptCustom.search(sqlCompareScriptSearchDto);
        List<SySqlCompareScriptEntity>  list = page.getContent();
        List<CompareScriptDto> compareScriptDtos = list.stream().parallel().map(t -> {
            List<SyCompareScriptKeyEntity> authors = scriptKeyRepository.findAllByScriptIdAndType(t.getScriptno(), 1);
            List<SyCompareScriptKeyEntity> variables = scriptKeyRepository.findAllByScriptIdAndType(t.getScriptno(), 2);
            return CompareScriptMappre.toCompareScriptDto(t, variables, authors);
        }).collect(Collectors.toList());


        PageableDto pageableDto = PageBaseMapper.pageExchange(page, compareScriptDtos);
        return ResponseEntity.ok(pageableDto);
    }





    public ResponseEntity saveOrUpdate(CompareScriptDto compareScriptDto){
        log.info("saveOrupdate CompareScriptDto:{}",compareScriptDto);

        if (null == compareScriptDto.getScriptno() ){
            compareScriptDto.setScriptno("sc"+ flakeIdGenerator.newId());
        }
        if (0 == compareScriptDto.getId() ){
            compareScriptDto.setId( flakeIdGenerator.newId());
        }

        SySqlCompareScriptEntity sqlCompareScriptEntity = CompareScriptMappre.toSqlCompareScriptEntity(compareScriptDto);


        SySqlCompareScriptEntity save = sqlCompareRepository.save(sqlCompareScriptEntity);

        List<SyCompareScriptKeyEntity> authors = compareScriptDto.getAuthors();
        authors.stream().forEach(t->t.setScriptId(save.getScriptno()));

        List<SyCompareScriptKeyEntity> variables = compareScriptDto.getVariables();
        variables.stream().forEach(t->t.setScriptId(save.getScriptno()));
        scriptKeyRepository.saveAll(authors);
        scriptKeyRepository.saveAll(variables);

        return  ResponseEntity.ok("success");
    }



@Transactional
    public ResponseEntity deleteScript(String scriptno){

        log.info("deleteScript:{}",scriptno);

        List<SyCompareTaskEntity> tasks = taskRepository.findAllByScriptId(scriptno);
        sqlCompareRepository.deleteAllByScriptno(scriptno);
        scriptKeyRepository.deleteAllByScriptId(scriptno);
        log.info("deleteScripttasks:{}",tasks);
        //taskRepository.deleteAllByScriptId(scriptno);
        tasks.stream().parallel().forEach(t->{
            taskService.deleteTask(t.getTaskno());
        });

    return  ResponseEntity.ok("success");


    }

    /**
     * 费弃
     * @param file
     * @return
     */
    public ResponseEntity saveFromExcel(MultipartFile file){
        log.info("save sqlCompareScriptEntity from excel--");
        try {

            ExcelOperte excelOperte = new ExcelOperte(file.getInputStream());
            List<ExcelReadSqlScriptDto>  list = excelOperte.getSheet(0, ExcelReadSqlScriptDto.class);

            list.stream().forEach(t->{
              //  t.getProjectName();
                long projectId = syProjectRepository.findByName(t.getProjectName().trim()).getId();
                SySqlCompareScriptEntity sqlCompareScriptEntity = new SySqlCompareScriptEntity();
                sqlCompareScriptEntity.setProjectid(projectId)
                        .setName(t.getName())
                      //  .setAnchor(t.getAnchor())
                        .setSrcSql(t.getSrcSql())
                        .setDestSql(t.getDestSql())
                        .setStatus(1);
                sqlCompareRepository.save(sqlCompareScriptEntity);
            });
            List<SySqlCompareScriptEntity> all = sqlCompareRepository.findAll();
            return ResponseEntity.ok(all);
        } catch (Exception e) {
            log.info("erreor:{}",e);
            return ResponseEntity.badRequest().body(e);
        }

    }
}


